package com.it.mydream.Controller;

import com.it.mydream.Bean.MsgBoard;
import com.it.mydream.Bean.User;
import com.it.mydream.Service.imp.LoginServiceImp;
import com.it.mydream.Service.imp.MsgBoardServiceImp;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HallController {
    @Autowired
    private LoginServiceImp loginService;
    @Autowired
    private MsgBoardServiceImp msgBoardService;

    @RequestMapping("getUserList")
    @ResponseBody
    public Map<String, Object> getUserList(){
        List<User> userlist = loginService.getUserList();
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",userlist.size());
        map.put("data",userlist);
        return map;
    }

    @RequestMapping("addMsgBoard")
    @ResponseBody
    public String addMsgBoard(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String msg_content = request.getParameter("msg_content");
        MsgBoard msgBoard = new MsgBoard();
        msgBoard.setUser(user);
        msgBoard.setMsg_content(msg_content);
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        msgBoard.setMsg_time(sdf.format(System.currentTimeMillis()));
        msgBoardService.addMsgBoard(msgBoard);

        ServletContext application = request.getServletContext();
        List<MsgBoard>msgBoards = msgBoardService.GetMsgBoards();
        application.setAttribute("msgBoards",msgBoards);
        return "1";
    }

}
